<?php
/*
**************************************************************************
*  File: include/class_mysql.php
*  (c) MGSPaste 2009-2011
*  By elwizard. All rights reserved.
*  http://www.desarrollo-mkportal.org
*
*  This program is free software under MGS License Agreement. 
*  You cannot distribute in any way. For modifications, please read 
*  MGS License Agreement for more information.
*
**************************************************************************
*/

if (!defined("IN_WEB")) {
    die ("Sorry !! You cannot access this file directly.");
}

class db_driver {

    var $obj = array ("dbname" => "", "dbuser" => "root", "dbpasswd" => "", "dbhost" => "localhost");
    var $q_id = "";
    var $db_connect_id = "";
    var $query_count = 0;

    // CONNECT WITH THE DATABASE | MYSQL_CONNECT
    function connect() {

			$this->db_connect_id = mysql_connect($this->obj['dbhost'], $this->obj['dbuser'], $this->obj['dbpasswd']);
      if (!mysql_select_db($this->obj['dbname'], $this->db_connect_id)) {
        echo ("ERROR: Cannot find database ".$this->obj['dbname']);
    	}
    	
    }
    
    // MYSQL_GET_SERVER_INFO
    function mysqlinfo() {
      return mysql_get_server_info();
    }
    
    // MYSQL_GET_CLIENT_INFO
    function mysqlclientinfo() {
      return mysql_get_client_info();
    }
    
    
    // MYSQL_GET_HOST_INFO
    function mysqlhostinfo() {
      return mysql_get_host_info();
    }
    
    // MYSQL_GET_PROTO_INFO
    function mysqlprotoinfo() {
      return mysql_get_proto_info();
    }
    
    // DO THE QUERIES | MYSQL_QUERY
    function query($query) {
    
      $this->q_id = mysql_query($query, $this->db_connect_id);

      if (!$this->q_id) {
        $error1 = mysql_error();
        $error2 .= mysql_errno();
        die ("ERROR: Database error.<br> Cannot execute the query: $query <br>MySql Error returned: $error1 <br>MySql Error code: $error2");
        exit;
      }
	    
	    $this->query_count++;
      return $this->q_id;
    }

    // MYSQL_FETCH_ARRAY
    function fetch_row($q_id = "") {

    	if ($q_id == "") {
    		$q_id = $this->q_id;
    	}

      $result = mysql_fetch_array($q_id, MYSQL_ASSOC);
      return $result;

    }
    
    // MYSQL_LIST_TABLES
    function list_tables() {
      return mysql_list_tables($this->obj['dbname']);
    }
    
    // MYSQL_LIST_TABLES
    function field_flags($result) {
      return mysql_field_flags($result, 0);
    }
    
    // MYSQL_GET_NUM_ROWS
    function get_num_rows() {
      return mysql_num_rows($this->q_id);
    }

    // MYSQL_GET_INSERT_ID
	  function get_insert_id() {
      return mysql_insert_id($this->db_connect_id);
    }
    
    // MYSQL_AFFECTED_ROWS
    function affected_rows() {
      return @mysql_affected_rows();
    }

    // MYSQL_FREE_RESULT
    function free_result($q_id="") {

   		if ($q_id == "") {
    		$q_id = $this->q_id;
		  }
    	@mysql_free_result($q_id);
    }
    
    // MYSQL_CLOSE_DB
    function close_db() {
        return mysql_close($this->db_connect_id);
    }
    
    // MYSQL_REAL_ESCAPE_STRING
    function real_escape_string($str) {
        return mysql_real_escape_string($str, $this->db_connect_id);
    }
    
    // MYSQL_ERROR
    function error($line) {
        return mysql_error($this->db_connect_id, $line);
    }
    
    // DISPLAY ERROR
    function display_error($error, $line = '', $die = false) {

    	echo "<b>Error: </b>".$error."<br /><b>Line: </b>" . ($line== '' ? 'Unknown' : $line) . "<br />";
      exit;
      
      return false;
       
    }


} // end class

global $db_name, $db_user, $db_passwd, $db_server;

$DB = new db_driver;
$DB->obj['dbname'] = $db_name;
$DB->obj['dbuser'] = $db_user;
$DB->obj['dbpasswd'] = $db_passwd;
$DB->obj['dbhost'] = $db_server;
  
// connect to database
$DB->connect();

?>
